package com.example.baserapitaxi;

import entidades.usuarios.Usuario;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewAnimator;

public abstract class BaseRegistroActivity extends Activity
{
	protected ViewAnimator animator;
	
	protected Usuario usuario;
	
	protected EditText campoNombre;
	protected EditText campoApellido;
	protected EditText campoEmail;
	protected EditText campoTelefono;
	protected EditText campoClave1;
	protected EditText campoClave2;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		construirAnimator();
		setContentView(animator);
		
		cambiarLayout(R.layout.activity_base_registro);
		
		campoNombre = (EditText) findViewById(R.id.campoNombreRegistro);
		campoApellido = (EditText) findViewById(R.id.campoApellidoRegistro);
		campoTelefono = (EditText) findViewById(R.id.campoTelefonoRegistro);
		campoEmail = (EditText) findViewById(R.id.campoEmailRegistro);
		campoClave1 = (EditText) findViewById(R.id.campoClave1Registro);
		campoClave2 = (EditText) findViewById(R.id.campoClave2Registro);
		
		instanciarUsuario();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		String telefono = getPhoneNumber();
		
		Log.i("LOG", "Telefono: " + telefono);
		
		campoTelefono.setText(telefono);
		usuario.setTelefono(telefono);
	}
	
	private void construirAnimator ()
	{
		Animation animation;
		
		animator = new ViewAnimator(this);
		animator.setAnimateFirstView(true);
		
		animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(2000);
		animator.setInAnimation(animation);
		
		animation = new AlphaAnimation(1.0f, 0.0f);
		animation.setDuration(2000);
		animator.setOutAnimation(animation);
	}
	
	public void aceptar (View view)
	{
		String check = check();
		
		if (check != null)
		{
			toast(check);
			return;
		}
		
		continuar();
	}

	public String check ()
	{
		construir();
		
		if (!checkClave())
			return "Claves incorrectas. Verifique que coinciden y tienen al menos 6 caracteres.";
		
		if (!checkString(usuario.getNombre()))
			return "Nombre incompleto.";
		
		if (!checkString(usuario.getApellido()))
			return "Apellido incompleto.";
		
		if (!checkString(usuario.getTelefono()))
			return "Telefono incompleto.";
		
		if (!checkString(usuario.getEmail()))
			return "Email incompleto.";
		
		return null;
	}
	
	private void construir ()
	{
		usuario.setNombre(campoNombre.getText().toString());
		usuario.setApellido(campoApellido.getText().toString());
		usuario.setTelefono(campoTelefono.getText().toString());
		usuario.setEmail(campoEmail.getText().toString());
		usuario.setClave(campoClave1.getText().toString());
	}
	
	private boolean checkClave ()
	{
		String clave1 = campoClave1.getText().toString();
		String clave2 = campoClave2.getText().toString();
		
		if (clave1 == null || clave1.equals(""))
			return false;
		
		if (clave2 == null || clave2.equals(""))
			return false;
		
		if (clave1.equals(clave2))
			return clave1.length() > 6;
			
		return false;
	}
	
	private boolean checkString (String check)
	{
		return check != null && !check.equals("");
	}
	
	protected void toast (String mensaje)
	{
		Toast.makeText(BaseRegistroActivity.this, mensaje, Toast.LENGTH_LONG).show();
	}
	
	protected void cambiarLayout (int id)
	{
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(id, null);
		
		animator.removeAllViews();
		animator.addView(view);
	}
	
	private String getPhoneNumber()
	{
		  TelephonyManager mTelephonyManager;
		  mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		  return mTelephonyManager.getLine1Number();
	}
	
	protected abstract void instanciarUsuario ();
	public abstract void continuar ();
}
